from typing import Any, Optional
from quart import jsonify
from src.common.models.response import ApiResponse, ResponseCode, ResponseMessage
from src.common.models.status import ResponseStatus
from src.common.middleware.request_id import get_request_id


class ResponseHelper:
    """
    响应工具类，简化控制器中的响应构建
    """
    
    @staticmethod
    def success(data: Any = None, message: str = ResponseMessage.SUCCESS) -> tuple:
        """
        构建成功响应
        
        Args:
            data: 响应数据
            message: 响应消息
            
        Returns:
            tuple: (响应JSON, HTTP状态码)
        """
        response = ApiResponse(
            code=ResponseCode.SUCCESS,
            message=message,
            data=data,
            request_id=get_request_id()
        )
        return jsonify(response.to_dict()), 200
    
    @staticmethod
    def error(code: int = ResponseCode.INTERNAL_ERROR, 
              message: str = ResponseMessage.INTERNAL_ERROR, 
              data: Any = None,
              http_status: int = 500) -> tuple:
        """
        构建错误响应
        
        Args:
            code: 业务错误码
            message: 错误消息
            data: 错误详情数据
            http_status: HTTP状态码
            
        Returns:
            tuple: (响应JSON, HTTP状态码)
        """
        response = ApiResponse(
            code=code,
            message=message,
            data=data,
            request_id=get_request_id()
        )
        return jsonify(response.to_dict()), http_status
    
    @staticmethod
    def bad_request(message: str = ResponseMessage.BAD_REQUEST, data: Any = None) -> tuple:
        """
        构建400错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.BAD_REQUEST,
            message=message,
            data=data,
            http_status=400
        )
    
    @staticmethod
    def unauthorized(message: str = ResponseMessage.UNAUTHORIZED, data: Any = None) -> tuple:
        """
        构建401错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.UNAUTHORIZED,
            message=message,
            data=data,
            http_status=401
        )
    
    @staticmethod
    def forbidden(message: str = ResponseMessage.FORBIDDEN, data: Any = None) -> tuple:
        """
        构建403错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.FORBIDDEN,
            message=message,
            data=data,
            http_status=403
        )
    
    @staticmethod
    def not_found(message: str = ResponseMessage.NOT_FOUND, data: Any = None) -> tuple:
        """
        构建404错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.NOT_FOUND,
            message=message,
            data=data,
            http_status=404
        )
    
    @staticmethod
    def validation_error(message: str = ResponseMessage.VALIDATION_ERROR, data: Any = None) -> tuple:
        """
        构建422验证错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.VALIDATION_ERROR,
            message=message,
            data=data,
            http_status=422
        )
    
    @staticmethod
    def business_error(message: str = ResponseMessage.BUSINESS_ERROR, data: Any = None) -> tuple:
        """
        构建460业务错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.BUSINESS_ERROR,
            message=message,
            data=data,
            http_status=460
        )
    
    @staticmethod
    def database_error(message: str = ResponseMessage.DATABASE_ERROR, data: Any = None) -> tuple:
        """
        构建数据库错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.DATABASE_ERROR,
            message=message,
            data=data,
            http_status=500
        )
    
    @staticmethod
    def redis_error(message: str = ResponseMessage.REDIS_ERROR, data: Any = None) -> tuple:
        """
        构建Redis错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.REDIS_ERROR,
            message=message,
            data=data,
            http_status=500
        )
    
    @staticmethod
    def external_api_error(message: str = ResponseMessage.EXTERNAL_API_ERROR, data: Any = None) -> tuple:
        """
        构建外部API错误响应
        """
        return ResponseHelper.error(
            code=ResponseCode.EXTERNAL_API_ERROR,
            message=message,
            data=data,
            http_status=500
        )


# 创建全局实例，方便使用
response = ResponseHelper()